package com.kalua.list.domainmodel.security;

import java.util.Arrays;

import com.google.common.base.Objects;
import com.kalua.list.domainmodel.UUIDIdentifier;
import com.kalua.list.domainmodel.geo.CountryCode;

public class UserImpl implements MutableUser {
	
	private static final long serialVersionUID = 5154670360510801236L;
	
	private EmailAddress id;
	private UUIDIdentifier accountReference;
	private String preferredName;
	private MobilePhoneNumber phoneNumber;
	private CountryCode countryCode;
	private boolean verified;
	
	private UserImpl() { }
	
	public UserImpl(	EmailAddress id,
								UUIDIdentifier accountReference,								 
								String prefferedName,
								boolean verified, 
								MobilePhoneNumber phoneNumber,
								CountryCode countryCode) {
		this();
		setId(id);
		setAccountReference(accountReference);
		setPreferredName(prefferedName);
		setVerified(verified);
		setPhoneNumber(phoneNumber);
		setCountryCode(countryCode);
	}

	@Override
	public UUIDIdentifier getAccountReference() { return this.accountReference; }
	@Override
	public void setAccountReference(UUIDIdentifier accountReference) 
	{ this.accountReference = accountReference; }
	
	@Override
	public String getName() { return getId().getValue(); }

	@Override
	public EmailAddress getId() { return this.id; }
	private void setId(EmailAddress id) { this.id = id; }

	@Override
	public String getPreferredName() { return this.preferredName; }
	private void setPreferredName(String name) { this.preferredName = name; }

	@Override
	public boolean isVerified() { return this.verified; }
	public void setVerified(boolean verified) { this.verified = verified; }
	
	@Override
	public MobilePhoneNumber getPhoneNumber() { return this.phoneNumber; }
	private void setPhoneNumber(MobilePhoneNumber phoneNumber) 
	{ this.phoneNumber = phoneNumber; }

	@Override
	public CountryCode getCountryOfResidence() { return this.countryCode; }
	private void setCountryCode(CountryCode countryCode) 
	{ this.countryCode = countryCode; }

	@Override
	public boolean equals(Object obj) {
		boolean result;
		
		if (this==obj) {
			result = true;
		} else if (obj instanceof UserImpl) {
			UserImpl other = (UserImpl) obj;
			if (!Objects.equal(this.id, other.id)) {
				Object[] fields = new Object[]{ this.accountReference, this.preferredName,
												this.verified,this.phoneNumber,
												this.countryCode};

				Object[] otherFields = new Object[]{other.accountReference, other.preferredName,
													other.verified,other.phoneNumber,
													other.countryCode};
				result = Arrays.equals(fields, otherFields);
			} else result = true;			
		} else result = false;
		
		return result;
	}
	
	@Override
	public String toString() 
	{ return "UserImpl [id=" + id + ", emailAddress=" + id + "]"; }
}